package org.vincen.helloworld.jdbc;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class DatabaseConnector {

	//返回Connection 对象
	public static Connection buildConnector () throws SQLException, IOException {
		Connection conn;
		ReadProperties rp = new ReadProperties();
		String[] s =  rp.getProperties();
		if (s.length != 0) {
			//弃用DriveManager，以后使用DataSource方式链接数据库。
//			conn = DriverManager.getConnection(s[0], s[1], s[2]);
			MysqlDataSource dataSource = new MysqlDataSource();
			dataSource.setUrl(s[0]);
			dataSource.setUser(s[1]);
			dataSource.setPassword(s[2]);
			conn = dataSource.getConnection();
			return conn;	
		} else {
			throw new SQLException("build database connection failure");
		}
	}
}
